package com.course.controller;

import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;

import com.course.db.ChooseCourse;
import com.course.db.CourseBasic;
import com.course.db.Pages;
import com.course.db.Student;
import com.et.mvc.JspView;

public class SearchController extends ApplicationController {
	public JspView courseList() throws Exception{
	
		CourseBasic course = new CourseBasic();
		System.out.println("courselist");
		JspView view = new JspView("search/courseList.jsp");
		
		ResultSet courseRs = course.course_list();
	       
	       List< Hashtable<String, String> > CourseListTemp = new ArrayList< Hashtable<String, String> >();
	       List< Hashtable<String, String> > CourseList = new ArrayList< Hashtable<String, String> >();
			try{
				String value="";
				ResultSetMetaData courseRsMd = courseRs.getMetaData();
				int cols = courseRsMd.getColumnCount();
				while (courseRs.next())
				{
					Hashtable<String, String> hash = new Hashtable<String, String>();
					for (int i = 1; i <= cols; i++) {	
						String field = (String) (courseRsMd.getColumnName(i));
						int fieldtype = courseRsMd.getColumnType(i);
						if(fieldtype == java.sql.Types.VARCHAR )
							value = (String) (courseRs.getString(i));
						else if(fieldtype == java.sql.Types.DOUBLE)
							value = (String)(Double.toString(courseRs.getDouble(i)));
						else if(fieldtype == java.sql.Types.INTEGER)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.BIGINT)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.SMALLINT)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.TINYINT)
							value = (String)(Integer.toString(courseRs.getInt(i)));
						else if(fieldtype == java.sql.Types.CHAR)
							value = (String) (courseRs.getString(i));
						else if(fieldtype == java.sql.Types.DATE)
							value = (String) (courseRs.getDate(i).toString());
						
						if (value == null)
							value = "";
						hash.put(field, value);
					}
					CourseListTemp.add(hash);
				}
				
				session.setAttribute("CourseList", CourseList);
				
			}catch(Exception e){
				e.printStackTrace();
			}
			
			Pages page = new Pages();
			String parameters = "page=page";
			page.setIntPageSize(10);
			page.InitPage(parameters,CourseListTemp.size(), request);
			String footer = page.PageFooter(); 
			session.setAttribute("CourseListPageFooter", footer);
			
			int intPageCount = page.getIntPageCount();
			int intPage = page.getIntPage();
			int intPageSize = page.getIntPageSize();
			
			Iterator<Hashtable<String, String>> user_it = CourseListTemp.iterator();
			if (intPageCount>0)  
	        {  
				System.out.println("intPageCount:"+intPageCount);
				System.out.println("intPageSize:"+intPageSize);
				System.out.println("intPage:"+intPage);
				
	           int nowpage=0; 
		    	while(user_it.hasNext())
				{
		    		Hashtable<String, String> userlist_hash  = user_it.next();
		    		nowpage++;
					if (nowpage>((intPage-1)*intPageSize) &&(nowpage<=intPage*intPageSize))  
		            {
						//System.out.println("nowpage:"+nowpage);
						Hashtable<String, String> hash = new Hashtable<String, String>();
						for(Iterator itr = userlist_hash.keySet().iterator(); itr.hasNext();)
						{
							String key = (String) itr.next();
							String value = (String) userlist_hash.get(key);

							if(value != null)
								hash.put(key, value);
							else
								hash.put(key, "");
						}

						CourseList.add(hash);
		            }
				}
	        }
		
		session.setAttribute("CourseList", CourseList);
			
		response.sendRedirect("../search/courseList.jspx");
		return null;
	}
	
	public JspView searchCourse() throws Exception{
		
		CourseBasic course = new CourseBasic();
		System.out.println("searchCourse");
		
		String course_type = request.getParameter("course_type");
		String grade = request.getParameter("grade");
		String studentId = request.getParameter("studentId");
		String src = request.getParameter("src");
		
		if(course_type == null)
			course_type = "全部";
		if(grade == null)
			grade = "全部";
		
		ResultSet courseRs = null;
		if( course_type.equals("全部"))
		{
			if(grade.equals("全部"))
			{
				courseRs = course.course_list();
			}
			else
			{
				courseRs = course.course_list_grade(grade);
			}
		}
		else
		{
			if(grade.equals("全部"))
			{
				courseRs = course.course_list_type(course_type);
			}
			else
			{
				courseRs = course.course_list_typegrade(course_type, grade);
			}
		}
		List< Hashtable<String, String> > CourseList = new ArrayList< Hashtable<String, String> >();
		try{
			String value="";
			ResultSetMetaData courseRsMd = courseRs.getMetaData();
			int cols = courseRsMd.getColumnCount();
			while (courseRs.next())
			{
				Hashtable<String, String> hash = new Hashtable<String, String>();
				for (int i = 1; i <= cols; i++) {	
					String field = (String) (courseRsMd.getColumnName(i));
					int fieldtype = courseRsMd.getColumnType(i);
					if(fieldtype == java.sql.Types.VARCHAR )
						value = (String) (courseRs.getString(i));
					else if(fieldtype == java.sql.Types.DOUBLE)
						value = (String)(Double.toString(courseRs.getDouble(i)));
					else if(fieldtype == java.sql.Types.INTEGER)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.BIGINT)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.SMALLINT)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.TINYINT)
						value = (String)(Integer.toString(courseRs.getInt(i)));
					else if(fieldtype == java.sql.Types.CHAR)
						value = (String) (courseRs.getString(i));
					else if(fieldtype == java.sql.Types.DATE)
						value = (String) (courseRs.getDate(i).toString());
					
					if (value == null)
						value = "";
					hash.put(field, value);
				}
				CourseList.add(hash);
			}
			
			if(src == null)
				session.setAttribute("CourseList", CourseList);
			else
				session.setAttribute("SelectiveCourseList", CourseList);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		if(course_type != null)
			session.setAttribute("coursetype", course_type);
		if(grade != null)
			session.setAttribute("grade", grade);
		if(studentId != null)
			session.setAttribute("StudentViewStuId", studentId);
		
		if(src == null)
			response.sendRedirect("../search/courseList.jspx");
		else
			response.sendRedirect("../admin/courseList.jspx");
		
		return null;
	}
	
public JspView searchStudent() throws Exception{
		
		Student student = new Student();
		System.out.println("searchCourse");
		String coursename = request.getParameter("coursename");
		String courseId = request.getParameter("courseId");
		String pages = request.getParameter("pages");
		String studentname=null, grade=null,selectstatus=null;
		if(pages == null)
		{
			studentname = request.getParameter("studentname");
			grade = request.getParameter("grade");
			selectstatus = request.getParameter("selectstatus");
		}
		else
		{
			studentname = request.getParameter("studentname");
			if(studentname!=null)
				studentname = new String(studentname.getBytes("iso8859-1"), "utf-8");
			grade = request.getParameter("grade");
			if(grade!=null)
				grade = new String(grade.getBytes("iso8859-1"),"utf-8");
			selectstatus = request.getParameter("selectstatus");
			if(selectstatus!=null)
				selectstatus = new String(selectstatus.getBytes("iso8859-1"),"utf-8");
		}
		
		session.setAttribute("studentname", studentname);
		session.setAttribute("grade", grade);
		session.setAttribute("selectstatus", selectstatus);
		ResultSet studentRs = null;
		request.setCharacterEncoding("UTF-8");
	    response.setCharacterEncoding("UTF-8");
	    response.setContentType("text/html;charset=UTF-8");
	       
		if( studentname == null || studentname.trim().equals(""))
		{
			if(grade.equals("全部"))
			{
				studentRs =student.show_students();
			}
			else
			{
				studentRs = student.show_grade_students(grade);
			}
		}
		else
		{
			if(grade.equals("全部"))
			{
				studentRs = student.show_name_students(studentname);
			}
			else
			{
				studentRs = student.show_students(studentname, grade);
			}
		}
		
		List< Hashtable<String, String> > StudentListTemp = new ArrayList< Hashtable<String, String> >();
		List< Hashtable<String, String> > StudentList = new ArrayList< Hashtable<String, String> >();
		try{
			String value="";
			ResultSetMetaData courseRsMd = studentRs.getMetaData();
			int cols = courseRsMd.getColumnCount();
			while (studentRs.next())
			{
				
				int studentId = studentRs.getInt("Id");
				
				ChooseCourse choosecourse = new ChooseCourse();
				int num = 0;
				num += choosecourse.type1_course_num(studentId);
				num += choosecourse.type2_course_num(studentId);
				num += choosecourse.type3_course_num(studentId);
				
				int checkednum = 0;
				checkednum += choosecourse.course_checked(studentId);
				
				if(!selectstatus.equals("全部"))
				{
					if(selectstatus.equals("未选"))
					{
						if(num<3)
						{
							Hashtable<String, String> hash = new Hashtable<String, String>();
							for (int i = 1; i <= cols; i++) {	
								String field = (String) (courseRsMd.getColumnName(i));
								int fieldtype = courseRsMd.getColumnType(i);
								if(fieldtype == java.sql.Types.VARCHAR )
									value = (String) (studentRs.getString(i));
								else if(fieldtype == java.sql.Types.DOUBLE)
									value = (String)(Double.toString(studentRs.getDouble(i)));
								else if(fieldtype == java.sql.Types.INTEGER)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.BIGINT)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.SMALLINT)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.TINYINT)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.CHAR)
									value = (String) (studentRs.getString(i));
								else if(fieldtype == java.sql.Types.DATE)
									value = (String) (studentRs.getDate(i).toString());
								
								if (value == null)
									value = "";
								hash.put(field, value);
								hash.put("selectstatus", "未选");
							}
							StudentListTemp.add(hash);
						}
					}
					else if(selectstatus.equals("已选"))
					{
						if(num>=3)
						{
							Hashtable<String, String> hash = new Hashtable<String, String>();
							for (int i = 1; i <= cols; i++) {	
								String field = (String) (courseRsMd.getColumnName(i));
								int fieldtype = courseRsMd.getColumnType(i);
								if(fieldtype == java.sql.Types.VARCHAR )
									value = (String) (studentRs.getString(i));
								else if(fieldtype == java.sql.Types.DOUBLE)
									value = (String)(Double.toString(studentRs.getDouble(i)));
								else if(fieldtype == java.sql.Types.INTEGER)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.BIGINT)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.SMALLINT)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.TINYINT)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.CHAR)
									value = (String) (studentRs.getString(i));
								else if(fieldtype == java.sql.Types.DATE)
									value = (String) (studentRs.getDate(i).toString());
								
								if (value == null)
									value = "";
								hash.put(field, value);
								hash.put("selectstatus", "已选");
							}
							StudentListTemp.add(hash);
						}
					}
					else if(selectstatus.equals("已审核"))
					{
						if(checkednum>0)
						{
							Hashtable<String, String> hash = new Hashtable<String, String>();
							for (int i = 1; i <= cols; i++) {	
								String field = (String) (courseRsMd.getColumnName(i));
								int fieldtype = courseRsMd.getColumnType(i);
								if(fieldtype == java.sql.Types.VARCHAR )
									value = (String) (studentRs.getString(i));
								else if(fieldtype == java.sql.Types.DOUBLE)
									value = (String)(Double.toString(studentRs.getDouble(i)));
								else if(fieldtype == java.sql.Types.INTEGER)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.BIGINT)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.SMALLINT)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.TINYINT)
									value = (String)(Integer.toString(studentRs.getInt(i)));
								else if(fieldtype == java.sql.Types.CHAR)
									value = (String) (studentRs.getString(i));
								else if(fieldtype == java.sql.Types.DATE)
									value = (String) (studentRs.getDate(i).toString());
								
								if (value == null)
									value = "";
								hash.put(field, value);
								hash.put("selectstatus", "已审核");
							}
							StudentListTemp.add(hash);
						}
					}
				}
				else
				{
					Hashtable<String, String> hash = new Hashtable<String, String>();
					for (int i = 1; i <= cols; i++) {	
						String field = (String) (courseRsMd.getColumnName(i));
						int fieldtype = courseRsMd.getColumnType(i);
						if(fieldtype == java.sql.Types.VARCHAR )
							value = (String) (studentRs.getString(i));
						else if(fieldtype == java.sql.Types.DOUBLE)
							value = (String)(Double.toString(studentRs.getDouble(i)));
						else if(fieldtype == java.sql.Types.INTEGER)
							value = (String)(Integer.toString(studentRs.getInt(i)));
						else if(fieldtype == java.sql.Types.BIGINT)
							value = (String)(Integer.toString(studentRs.getInt(i)));
						else if(fieldtype == java.sql.Types.SMALLINT)
							value = (String)(Integer.toString(studentRs.getInt(i)));
						else if(fieldtype == java.sql.Types.TINYINT)
							value = (String)(Integer.toString(studentRs.getInt(i)));
						else if(fieldtype == java.sql.Types.CHAR)
							value = (String) (studentRs.getString(i));
						else if(fieldtype == java.sql.Types.DATE)
							value = (String) (studentRs.getDate(i).toString());
						
						if (value == null)
							value = "";
						hash.put(field, value);
					}
					if(num<3)
					{
						hash.put("selectstatus", "未选");
					}
					else if(num>=3)
					{
						if(checkednum>0)
							hash.put("selectstatus", "已审核");
						else
							hash.put("selectstatus", "已选");
					}
					StudentListTemp.add(hash);
				}
			}
			
			session.setAttribute("NewStudentList", StudentListTemp);
			
		}catch(Exception e){
			e.printStackTrace();
		}
		
		session.setAttribute("coursename", coursename);
		session.setAttribute("courseId", courseId);
		
		String src = request.getParameter("src");
		
		if(src == null)
			response.sendRedirect("../admin/studentList.jspx");
		else
		{
			CourseBasic course = new CourseBasic();
			ResultSet courseRs = course.course_list();
		       
		       List< Hashtable<String, String> > CourseList = new ArrayList< Hashtable<String, String> >();
				try{
					String value="";
					ResultSetMetaData courseRsMd = courseRs.getMetaData();
					int cols = courseRsMd.getColumnCount();
					while (courseRs.next())
					{
						Hashtable<String, String> hash = new Hashtable<String, String>();
						for (int i = 1; i <= cols; i++) {	
							String field = (String) (courseRsMd.getColumnName(i));
							int fieldtype = courseRsMd.getColumnType(i);
							if(fieldtype == java.sql.Types.VARCHAR )
								value = (String) (courseRs.getString(i));
							else if(fieldtype == java.sql.Types.DOUBLE)
								value = (String)(Double.toString(courseRs.getDouble(i)));
							else if(fieldtype == java.sql.Types.INTEGER)
								value = (String)(Integer.toString(courseRs.getInt(i)));
							else if(fieldtype == java.sql.Types.BIGINT)
								value = (String)(Integer.toString(courseRs.getInt(i)));
							else if(fieldtype == java.sql.Types.SMALLINT)
								value = (String)(Integer.toString(courseRs.getInt(i)));
							else if(fieldtype == java.sql.Types.TINYINT)
								value = (String)(Integer.toString(courseRs.getInt(i)));
							else if(fieldtype == java.sql.Types.CHAR)
								value = (String) (courseRs.getString(i));
							else if(fieldtype == java.sql.Types.DATE)
								value = (String) (courseRs.getDate(i).toString());
							
							if (value == null)
								value = "";
							hash.put(field, value);
						}
						CourseList.add(hash);
					}
					
					session.setAttribute("AdminCourseList", CourseList);
					
				}catch(Exception e){
					e.printStackTrace();
				}
				
				//page
				Pages page = new Pages();
				String parameters = "";
				parameters = "src=admin";
				if(grade!=null)
					parameters += "&grade="+session.getAttribute("grade");
				if(studentname!=null&&!studentname.trim().equals(""))
					parameters += "&studentname="+session.getAttribute("studentname");
				if(selectstatus!=null&&!selectstatus.trim().equals(""))
					parameters += "&selectstatus="+session.getAttribute("selectstatus");
				
				page.setIntPageSize(20);
				page.InitPage(parameters,StudentListTemp.size(), request);
				String footer = page.PageFooter(); 
				session.setAttribute("SearchStuPageFooter", footer);
				
				int intPageCount = page.getIntPageCount();
				int intPage = page.getIntPage();
				int intPageSize = page.getIntPageSize();
				
				Iterator<Hashtable<String, String>> user_it = StudentListTemp.iterator();
				if (intPageCount>0)  
		        {  
					System.out.println("intPageCount:"+intPageCount);
					System.out.println("intPageSize:"+intPageSize);
					System.out.println("intPage:"+intPage);
					
		           int nowpage=0; 
			    	while(user_it.hasNext())
					{
			    		Hashtable<String, String> userlist_hash  = user_it.next();
			    		nowpage++;
						if (nowpage>((intPage-1)*intPageSize) &&(nowpage<=intPage*intPageSize))  
			            {
							Hashtable<String, String> hash = new Hashtable<String, String>();
							for(Iterator itr = userlist_hash.keySet().iterator(); itr.hasNext();)
							{
								String key = (String) itr.next();
								String value = (String) userlist_hash.get(key);

								if(value != null)
									hash.put(key, value);
								else
									hash.put(key, "");
							}

							StudentList.add(hash);
			            }
					}
		        }
			
			session.setAttribute("AdminStudentList", StudentList);
			response.sendRedirect("../admin/coursechooseManage.jspx?tab=1");
		}
		
		return null;
	}
}
